package com.div.tools.viewpager;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.viewpager.widget.ViewPager;


import java.lang.reflect.Field;

/**
 * author：wangrui
 * date：2020/5/18
 * description:类描述
 */
public class ViewPagerSlide extends ViewPager {
    private static final float MIN_FLING_VELOCITY = 320F;
    //是否可以进行滑动
    private boolean isCanScroll = false;
    private final String TAG = "ViewPagerSlide";
    private float downY;
    private float downX;
    private OnCloseWindowListener mListener;

    public ViewPagerSlide(Context context) {
        super(context);
    }

    public ViewPagerSlide(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 解决每次进入主页面后，首次切换到后排页面，smoothScroll 为 true 但无滑动效果的问题。
     * @param firstLayout
     */
    public void setmFirstLayout(boolean firstLayout) {
        try {
            Class clazz = this.getClass();
            Class<?> superclass = clazz.getSuperclass();
            Field mFirstLayout = null;
            if (superclass != null) {
                mFirstLayout = superclass.getDeclaredField("mFirstLayout");
                mFirstLayout.setAccessible(true);
                mFirstLayout.setBoolean(this, firstLayout);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean getmFirstLayout() {
        try {
            Class clazz = this.getClass();
            Class<?> superclass = clazz.getSuperclass();
            Field mFirstLayout = null;
            if (superclass != null) {
                mFirstLayout = superclass.getDeclaredField("mFirstLayout");
                mFirstLayout.setAccessible(true);
                return (boolean) mFirstLayout.get(this);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 设置其是否能滑动换页
     * @param isCanScroll false 不能换页， true 可以滑动换页
     */
    public void setScanScroll(boolean isCanScroll) {
        this.isCanScroll = isCanScroll;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return isCanScroll && super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
//        int action = ev.getAction();
//        if (action == MotionEvent.ACTION_DOWN) {
//            downX = ev.getX();
//            downY = ev.getY();
//        }else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
//            float upX = ev.getX();
//            float upY = ev.getY();
//            final float yDiff = Math.abs(upY - downY);
//            if (upY - downY >= MIN_FLING_VELOCITY && yDiff * 0.5f > Math.abs(upX - downX)) {
//                if (mListener != null) {
//                    mListener.onCloseWindow();
//                }
//                return false;
//            }
//        }
        return isCanScroll && super.onTouchEvent(ev);
    }

    public void setOnCloseWindowListener(OnCloseWindowListener listener) {
        this.mListener = listener;
    }

    public interface OnCloseWindowListener {
        void onCloseWindow();
    }
}
